﻿using CSharpLogger.Product;
using System.Threading;

namespace CSharpLogger
{
    /// <summary>
    /// A Logger object is used to log messages for a specific system or application component
    /// </summary>
    public class Logger
    {
        #region [ Public Methods ]
        /// <summary>
        /// Logging messages will be forwarded to registered ILoggerHandler concrect object, which can forward the messages to a variety of destinations, 
        /// including text files, database, email and/or event viewer.
        /// </summary>
        /// <param name="application">Application Name</param>
        /// <param name="host">Host Name or IP Address</param>
        /// <param name="message">Message to be logged</param>
        /// <param name="path">Shared Folder where the log file will be stored</param>
        /// <param name="username">User name logged into the application</param>
        /// <param name="msgLevel">Level established for the message to be logged</param>
        /// <param name="type">Type of Logger instance</param>
        /// <param name="logLevel">Level for the Logger instance</param>
        public static void Log(string application, string host, string message, string path, string username, Level msgLevel, Type type, Level logLevel)
        {
            if (msgLevel <= logLevel)
            {
                ILoggerHandler logger;
                
                if ((type & Type.BD) == Type.BD)
                {
                    logger = DBLogger.Log2DB;
                    logger.AppName = application;
                    logger.Host = host;
                    logger.Message = message;
                    logger.UserName = username;
                    logger.Level = logLevel;
                    logger.Log();
                }
                if ((type & Type.EMAIL) == Type.EMAIL)
                {
                    logger = EmailLogger.Log2Email;
                    logger.AppName = application;
                    logger.Host = host;
                    logger.Message = message;
                    logger.UserName = username;
                    logger.Level = logLevel;
                    logger.Log();
                }
                if ((type & Type.EVENTVIEWER) == Type.EVENTVIEWER)
                {
                    logger = EventViewerLogger.Log2EV;
                    logger.AppName = application;
                    logger.Host = host;
                    logger.Message = message;
                    logger.UserName = username;
                    logger.Level = logLevel;
                    logger.Log();
                }
                if ((type & Type.FILE) == Type.FILE)
                {
                    logger = FileLogger.Log2File;
                    logger.AppName = application;
                    logger.Host = host;
                    logger.Message = message;
                    logger.UserName = username;
                    logger.Level = logLevel;
                    logger.Log();
                }
            }
        }
        #endregion        
    }
}
